<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 新增角色按钮 -->
        <el-row style="height:60px">
          <el-button icon="el-icon-plus" type="primary" @click="newRole">新增角色</el-button>
        </el-row>
        <!-- 表格 -->
        <el-table :data="rowsList" style="width: 100%">
          <el-table-column type="index" :index="indexMethod" label="序号" width="180" />
          <el-table-column prop="name" label="角色名称" width="180" />
          <el-table-column prop="description" label="描述" />
          <el-table-column label="操作">
            <template v-slot="scope">
              <el-button @click="distriBution(scope.row.id)">分配权限</el-button>
              <el-button @click.native="editList(scope.row.id)">编辑</el-button>
              <el-button type="danger" @click.native="delList(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <!-- 分页组件 -->
          <el-pagination :current-page="params.page" :page-sizes="[5, 6, 8, 10]" :page-size="params.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange">
            />
          </el-pagination>
        </el-row>
      </el-card>
    </div>
    <el-dialog title="编辑弹层" :visible.sync="dialogVisible" width="30%" @close="closeDialog">
      <el-form ref="form" label-width="80px" :model="addForm" :rules="rules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="addForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="addForm.description" />
        </el-form-item>
        <template>
          <el-form-item>
            <el-button type="primary" @click="deterMine">确定</el-button>
            <el-button @click="canCel">取消</el-button>
          </el-form-item>
        </template>
      </el-form>

    </el-dialog>
    <!-- 分配权限的弹层 -->
    <el-dialog
      title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
      :visible="showAssignDialog"
      @close="closePddDialog"
      @open="hGetPermissionList"
    >
      <el-tree
        ref="perTree"
        :data="permissionList"
        :props="{ label: 'name' }"
        :default-expand-all="true"
        :show-checkbox="true"
        :check-strictly="true"
        node-key="id"
      />
      <el-checkbox-group v-model="checkList">
        <el-checkbox label="复选框 A" />
        <el-checkbox label="复选框 B" />
        <el-checkbox label="复选框 C" />
        <el-checkbox label="禁用" disabled />
        <el-checkbox label="选中且禁用" disabled />
      </el-checkbox-group>
      <!-- 权限点数据展示 -->
      <template #footer>
        <div style="text-align: right;">
          <el-button @click="closeAssignDialog">取消</el-button>
          <el-button type="primary" @click="clickAssign">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList, addRoleList, delRoleList, getRoleDetail, putRoleList, assignPerm } from '@/api/Setting'
import { getPermissionList } from '@/api/Permission'
import transTree from '@/utils/transTree'
export default {
  data() {
    return {
      params: {
        page: 1,
        pagesize: 5
      },
      rowsList: [],
      total: 0,
      dialogVisible: false,
      showAssignDialog: false,
      permissionList: [],
      curId: null,
      addForm: {
        name: '',
        description: '',
        id: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: ['blur', 'change'] }]
      }
    }
  },
  mounted() {
    this.fetchRoleList()
  },
  methods: {
    indexMethod(index) {
      return (this.params.page - 1) * this.params.pagesize + index + 1
    },
    async fetchRoleList() {
      const res = await getRoleList(this.params)
      this.rowsList = res.rows
      this.total = res.total
      console.log(this.rowsList)
    },
    handleSizeChange(val) {
      this.params.pagesize = val
      this.params.page = 1
      this.fetchRoleList()
    },
    handleCurrentChange(val) {
      this.params.page = val
      this.fetchRoleList()
    },
    // 点击新增角色按钮
    newRole() {
      this.dialogVisible = true
    },
    closeDialog() {
      this.dialogVisible = false
      this.$refs.form.resetFields()
    },
    // 取消按钮
    canCel() {
      this.dialogVisible = false
      this.$refs.form.resetFields()
    },
    // 确定按钮
    async deterMine() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          if (this.addForm.id) {
            await putRoleList(this.addForm)
          } else {
            await addRoleList(this.addForm)
          }

          // 提示
          this.$message.success(`${this.addForm.id ? '编辑' : '新增'}部门成功`)
          // 隐藏窗口
          this.dialogVisible = false
          // 渲染页面
          this.fetchRoleList()
          // 清空里边数据
          this.$refs.form.resetFields()
          this.addForm = {
            name: '',
            description: ''
          }
        }
      })
    },
    // 删除按钮
    delList(id) {
      this.$confirm('此操作将删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delRoleList(id)
        this.$message.success('删除成功')
        this.fetchRoleList()
      }).catch(() => {

      })
    },
    // 修改按钮
    async editList(id) {
      this.dialogVisible = true
      // console.log(id)
      const res = await getRoleDetail(id)
      this.addForm = res
    },
    // 分配权限按钮
    distriBution(id) {
      this.showAssignDialog = true
      this.curId = id
    },
    // 点击退出按钮、
    closePddDialog() {
      this.showAssignDialog = false
    },
    // open按钮
    async  hGetPermissionList(id) {
      const res = await getPermissionList()
      this.permissionList = transTree(res)
      const detail = await getRoleDetail(this.curId)
      console.log(detail)
      // 通过设置tree方法进行id回显
      this.$refs.perTree.setCheckedKeys(detail.permIds)
    },
    // 确定按钮
    async clickAssign() {
      // 1. 准备参数
      // id: 要给哪个角色分配 角色id  this.curId
      // permIds: 由我们选中项的权限id组成的数组  getCheckedKeys()
      const ids = this.$refs.perTree.getCheckedKeys()
      console.log(ids)
      // 2. 调用接口
      await assignPerm({
        id: this.curId,
        permIds: ids
      })
      this.$message.success('分配成功')
      this.showAssignDialog = false
    }

  }
}

</script>
